package com.itheima.huikebackend.service.impl;

import com.itheima.huikebackend.mapper.SysDeptMapper;
import com.itheima.huikebackend.service.SysDeptService;
import com.itheima.huikebackend.vo.DeptViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @program: huike
 * @className: SysDeptServiceImpl
 * @description: TODO
 * @author: xch
 * @createTime: 2023-08-08 19:41
 **/
@Service
public class SysDeptServiceImpl implements SysDeptService {

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Override
    public List<DeptViewVO> selectTree() {
        List<DeptViewVO> parentList = sysDeptMapper.selectTree(0L);

        parentList.forEach(this::selectTreeParent);

        return parentList;
    }

    private void selectTreeParent(DeptViewVO deptViewVO){
        List<DeptViewVO> childrenList = sysDeptMapper.selectTree(deptViewVO.getId());
        if (!Objects.isNull(childrenList)){
           childrenList.forEach(this::selectTreeParent);
        }
        deptViewVO.setChildren(childrenList);
    }
}
